package com.example.hw.testcling;

import android.util.Log;

import org.fourthline.cling.model.meta.Action;
import org.fourthline.cling.model.meta.ActionArgument;
import org.fourthline.cling.model.meta.Device;
import org.fourthline.cling.model.meta.Service;
import org.fourthline.cling.model.meta.StateVariable;

/**
 * Created by hw on 2018/7/17.
 */

class DeviceDisplay {
    private Device device;

    public DeviceDisplay(Device device) {
        this.device = device;
    }

    public Device getDevice() {
        return device;
    }

    public Service getService() {
        for (Service service : device.getServices()) {
            Log.d("TestCling", "Service: "+service.getServiceType().toFriendlyString());
        }
        return device.getServices()[0];
    }

    public String getDetailsMessage() {
        StringBuilder sb = new StringBuilder();
        if (getDevice().isFullyHydrated()) {

            Log.d("TestCling", "\n" +
                    getDevice().getDetails().getFriendlyName() + "\n" +
                    getDevice().getDetails().getManufacturerDetails().getManufacturer() + "\n" +
                    getDevice().getDetails().getManufacturerDetails().getManufacturerURI() + "\n" +
                    getDevice().getDetails().getModelDetails().getModelDescription() + "\n" +
                    getDevice().getDetails().getModelDetails().getModelName() + "\n" +
                    getDevice().getDetails().getModelDetails().getModelNumber() + "\n" +
                    getDevice().getDetails().getModelDetails().getModelURI() + "\n" +
                    getDevice().getDetails().getSerialNumber() + "\n");
            String sVersion = "" + getDevice().getVersion().getMajor() + " " + getDevice().getVersion().getMinor();
            Log.d("TestCling", "getDetailsMessage: Version = " + sVersion);

            String sDisplayString = getDevice().getDisplayString();
            Log.d("TestCling", "getDetailsMessage: DisplayString = " + sDisplayString);
            sb.append(sDisplayString);
            sb.append("\n\n");
            Log.d("TestCling", "\n\n");
            Log.d("TestCling", "getDetailsMessage========================================");
            for (Service service : getDevice().getServices()) {

                for (Action action : service.getActions()) {
                    Log.d("TestCling", "Action: " + action.getName());
                    for (ActionArgument actionArgument: action.getArguments()) {
                        Log.d("TestCling", "ActionArgument: " + actionArgument.getName() + " " + actionArgument.getDatatype().getDisplayString() + " " + actionArgument.getDirection().name());
                    }
                }
                for (StateVariable stateVariable : service.getStateVariables()) {
                    Log.d("TestCling", "StateVariable: " + stateVariable.getName() + " " + stateVariable.getTypeDetails().getDatatype().getDisplayString() + " " + stateVariable.getEventDetails().isSendEvents());
                }

                String sServiceType = service.getServiceType().toString();
                Log.d("TestCling", "getDetailsMessage: ServiceType = " + sServiceType + "  " + service.getServiceId().getId() + "  " + service.getServiceId().getNamespace());

                sb.append(sServiceType).append("\n");
            }
            Log.d("TestCling", "getDetailsMessage========================================");
        } else {
            sb.append("");
        }
        return sb.toString();
    }
    // DOC:DETAILS

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        DeviceDisplay that = (DeviceDisplay) o;
        return device.equals(that.device);
    }

    @Override
    public int hashCode() {
        return device.hashCode();
    }

    @Override
    public String toString() {
        String name =
                getDevice().getDetails() != null && getDevice().getDetails().getFriendlyName() != null
                        ? getDevice().getDetails().getFriendlyName()
                        : getDevice().getDisplayString();
        // Display a little star while the device is being loaded (see performance optimization earlier)
        return device.isFullyHydrated() ? name : name + " *";
    }
}
